<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Personal extends Model
{
    protected $table = 'personal';//用户管理
    public $timestamps = false;
    protected $guarded = [
       ];
    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }
    //个人中心列表
    public function getData($search = [], $page = 1, $length = 20){
        $userList = array();
        $userList['pageData'] = array();
        $userModel = new User();
        $sql = \DB::table($userModel->getTable())
            ->whereNull('deleted_at');

        if(isset($search['nick_name'])){
            $sql->where('nick_name', 'like', "%".$search['nick_name']."%");
        }

        if(isset($search['id'])){
            $sql->where('id', $search['id']);
        }

        $userList['pageData']['count']     = $sql->count();
        $userList['pageData']['lastPage']  = ceil($userList['pageData']['count'] / $length);

        $userList['list'] = $sql->skip(($page-1) * $length)->take($length)
            ->get();

        return $userList;
    }

    //个人中心列表
    public function getPersonalData($search = [], $page = 1, $length = 20){
        $userList = array();
        $userList['pageData'] = array();
        $userModel = new User();
        $sql = \DB::table($this->table)
            ->whereNull('deleted_at');

        if(isset($search['nick_name'])){
            $sql->where('nick_name', 'like', "%".$search['nick_name']."%");
        }

        if(isset($search['id'])){
            $sql->where('id', $search['id']);
        }

        if(isset($search['u_id'])){
            $sql->where('u_id', $search['u_id']);
        }

        $userList['pageData']['count']     = $sql->count();
        $userList['pageData']['lastPage']  = ceil($userList['pageData']['count'] / $length);

        $userList['list'] = $sql->skip(($page-1) * $length)->take($length)
            ->get();

        return $userList;
    }

    //积分修改
    public function saveEdit($data,$id){
        $userModel = new User();
        return \DB::table($userModel->getTable())->where('id',$id)->update($data);
    }
    //个人中心删除
    public function Del($data,$id){
        $userModel = new User();
        return \DB::table($userModel->getTable())->where('id',$id)->update($data);
    }
}